package com.lemon.bean.stageBean;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


public class LoadPlayLight {
    //幻灯片播放窗口
    private VBox playLightWindow = null;
    private Stage stage = new Stage();
    private Tooltip tooltip ;
    public void loadPlayLight(){
        try {
            playLightWindow = FXMLLoader.load(getClass().getResource("PlayLight.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene = new Scene(playLightWindow,600,600);
        stage.setScene(scene);
//        stage.initModality(Modality.APPLICATION_MODAL);
        stage.toFront();
        playLightWindow.setAlignment(Pos.CENTER);
        ImageView iv = new ImageView();
        iv.setImage(new Image(stringList.get(curIndex)));
        iv.fitWidthProperty().bind(playLightWindow.widthProperty());
        iv.fitHeightProperty().bind(playLightWindow.heightProperty());
        iv.setPreserveRatio(true);
        playLightWindow.getChildren().add(iv);
        stage.addEventHandler(KeyEvent.KEY_PRESSED,keyEvent -> {
            System.out.println(keyEvent.getCode()==KeyCode.ESCAPE);
            if(keyEvent.getCode()== KeyCode.ESCAPE){
                stage.setFullScreen(false);
                timeline.stop();
                close();
            }
        });
        URL resource = this.getClass().getResource("css/playLight.css");
        addStyle(resource.toExternalForm());
    }

    private Timeline timeline =null;
    private int curIndex;
    private List<String> stringList = new ArrayList<>();
    public void initFunction(){

    }
    //播放
    public void play(){
        if(stringList.isEmpty()){
            close();
        }
        timeline= new Timeline(new KeyFrame(Duration.seconds(1.5),e->{
            nextStep();
        }));
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.play();
    }
    private void nextStep(){
        curIndex++;
        if(curIndex == stringList.size()){
            //提示 toDo
            tooltip = new Tooltip();
            tooltip.setFont(new Font(20));
            tooltip.setX(stage.getWidth()/2-150);
            tooltip.setY(20);
            tooltip.setText("             最后一张啦！");
            //todo
            tooltip.setTextAlignment(TextAlignment.JUSTIFY);
            tooltip.setPrefWidth(300);
            tooltip.setPrefHeight(100);
            tooltip.show(stage);
        }
        if(curIndex<stringList.size()){
            Image image = new Image(stringList.get(curIndex));
            ImageView node = (ImageView) playLightWindow.getChildren().get(0);
            node.setImage(image);
        }
        //延迟关闭
        if(curIndex>=stringList.size()+1){
            close();
        }
    }
    public void setCurIndex(int curIndex){
        this.curIndex=curIndex;
    }

    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }

    public void fullScene(boolean yes){
        stage.setFullScreen(yes);
    }

    public void show(){
        stage.show();
    }
    public void close(){
        timeline.stop();
        timeline=null;
        stage.close();
    }
    public void addStyle(String cssPath){
        playLightWindow.getStylesheets().add(cssPath);
    }
}
